package org.example.controller;

import org.example.entity.User;
import org.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    // 前端 JSP 页面
    @GetMapping("/userList")
    public String userList() {
        return "userList"; // /WEB-INF/views/userList.jsp
    }

    // DataTables Scroller Ajax 接口
    @PostMapping("/userData")
    @ResponseBody
    public Map<String, Object> userData(
            @RequestParam("draw") int draw,
            @RequestParam("start") int start,
            @RequestParam("length") int length
    ) {
        System.out.println("draw = " + draw +" start "+ start +" length "+ length );
        int totalCount = userService.getTotalCount();
        List<User> users = userService.getUserList(start, length);

        Map<String, Object> result = new HashMap<>();
        result.put("draw", draw);
        result.put("recordsTotal", totalCount);
        result.put("recordsFiltered", totalCount);
        result.put("data", users);
        return result;
    }
}
